/**
 * Copyright (c) 2011-9-23 WKM and/or its affiliates. 
 * Use is subject to license terms.
 * All rights reserved.
 */
package org.wkm.util;

import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * SystemUtil 系统信息相关操作
 * @version 1.0 2012-1-17
 * @author WUKUNMENG
 */
public class SystemUtil {
	
	private static Logger logger = LoggerFactory.getLogger(SystemUtil.class);

	private SystemUtil () {
	}
	
	//32位操作系统
	public final static String OS_32 = "32";
	
	//64位操作系统
	public final static String OS_64 = "64";
	
	//当前虚拟机的位数
	public final static String VM_CUR_BIT = System.getProperty("sun.arch.data.model");

	//当前操作系统名称
	public final static String OS_NAME = System.getProperty("os.name");
	
	/**
	 * 判断当前 Java VM 是否32位
	 * @return
	 */
	public static boolean isOS32(){
		return OS_32.equals(VM_CUR_BIT);
	}
	
	/**
	 * 判断当前 Java VM 是否64位
	 * @return
	 */
	public static boolean isOS64(){
		return OS_64.equals(VM_CUR_BIT);
	}
	
	
	/**
	 * 判断当前操作系统是否为Linux系统
	 * @return
	 */
	public static boolean isLinux(){
		return L_NAME.equals(System.getProperty("osystem.name"));
	}
	
	/**
	 * 判断当前操作系统是否为Windows系统
	 * @return
	 */
	public static boolean isWindows(){
		return W_NAME.equals(System.getProperty("osystem.name"));
	}
	
	//windows 通配符
	private final static String WIN = "window*";
	
	//Linux 通配符
	private final static String LINUX = "linux*";
	
	//windows名称
	public final static String W_NAME = "windows";
	
	//Linux名称
	public final static String L_NAME = "linux";
	
	//其他操作系统
	public final static String UNKNOW = "unknow";
	
	/**
	 * 初始化当前操作系统信息！
	 */
	static {
		
		logger.info("初始化本地操作系统基本信息...");
		
		Pattern win = Pattern.compile(WIN, Pattern.CASE_INSENSITIVE);
		
		Pattern lin = Pattern.compile(LINUX, Pattern.CASE_INSENSITIVE);
		
		logger.info("获取当前操作系统是：" + OS_NAME);
		
		if(win.matcher(OS_NAME).find()){
			System.setProperty("osystem.name", W_NAME);
		} 
		
		if(lin.matcher(OS_NAME).find()){
			System.setProperty("osystem.name", L_NAME);
		}
		
		if(System.getProperty("osystem.name") == null) {
			System.setProperty("osystem.name", UNKNOW);
		}
		
		logger.info("判断当前操作系统是：" + System.getProperty("osystem.name"));
		logger.info("判断当前Java虚拟机位数：" + VM_CUR_BIT);
	}
}


